using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Threading;

namespace Adoor.Wizard
{
    public partial class FormTypeConvertion : Form
    {
        public FormTypeConvertion()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            
            DataTable.AcceptChanges();
        }

        private DataTable dataTable;

        public DataTable DataTable
        {
            get { return dataTable; }
            set 
            { 
                dataTable = value;
                dataGridView1.DataSource = value;
            }
        }

        private string xmiFilePath;

        public string XmiFilePath
        {
            get { return xmiFilePath; }
            set { xmiFilePath = value; }
        }

        private void buttonImportXmi_Click(object sender, EventArgs e)
        {
            startImportFromXmi();
        }

        private void startImportFromXmi()
        {
            wait = true;
            WaitCallback wc = new WaitCallback(import);
            wc.BeginInvoke(null,
                new AsyncCallback(endImportFromXmi),
                wc);
        }

        private void endImportFromXmi(IAsyncResult result)
        {
            try
            {
                WaitCallback wc = (WaitCallback)result.AsyncState;
                wc.EndInvoke(result);

               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
               
            }
            finally
            {
                wait = false;
            }
        }

        private void import(object state)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(XmiFilePath);
            
            XmlNode xmiNode = doc.SelectSingleNode("/XMI");
            string uml = xmiNode.GetNamespaceOfPrefix("UML");
            
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
            if (!string.IsNullOrEmpty(uml))
                nsmgr.AddNamespace("UML", uml);

            string defaultVal = string.Empty;
            List<string> conditions = new List<string>(DataTable.Rows.Count);
            foreach (DataRow dr in DataTable.Rows)
            {
                string cond = dr[0].ToString();
                if (string.Compare(cond, " default", true) != 0)
                {
                    conditions.Add(cond);
                }
                else
                    defaultVal = dr[1].ToString();

            }

            StringBuilder sb = new StringBuilder();
            sb.Append("//UML:DataType[@name");
            if (conditions.Count > 0)
            {
                sb.Append(" and not(");
                sb.Append(string.Join(" or ", conditions.ToArray()));
                sb.Append(" )");
            }
            sb.Append(" ] ");

            foreach (XmlElement node in doc.SelectNodes(sb.ToString(), nsmgr))
            {
                Invoke(new ThreadStart(delegate()
                {
                    DataTable.Rows.Add(new object[] { string.Format("@name='{0}'", node.GetAttribute("name")), defaultVal });
                }));
            }
        }

        private int waitCnt;
        private bool wait
        {
            get
            {
                return waitCnt != 0;
            }
            set
            {
                bool oldState = wait;
                if (value)
                    waitCnt++;
                else
                    waitCnt--;
                if (oldState != wait)
                    onWaitChanged();
            }
        }

        private void onWaitChanged()
        {
            Invoke(new ThreadStart(delegate()
            {
                bool newWait = wait;
                foreach (Control ctrl in Controls)
                {
                    ctrl.Enabled = !wait;
                }
                UseWaitCursor = wait;
            }));
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            DataTable.AcceptChanges();
            DialogResult = DialogResult.OK;
            Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DataTable.RejectChanges();
            DialogResult = DialogResult.Cancel;
            Close();
        }

    }
}